package com.kaigejava.fanoutexchange;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * Created by kaigejava on 2019/7/14.
 * fanout exchange 生产者
 */
public class Producer4FanOutExchange {
    public static void main(String[] args) throws  Exception{
        //1:创建一个connectioFactory工厂对象，并进行配置
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //设置ip 端口 vhost等
        connectionFactory.setHost("192.168.31.130");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        //2：通过工厂对象获取到connection对象
        Connection connection = connectionFactory.newConnection();

        //3：通
        // 过connection对象获取到一个消息通信的通道 channel
        Channel channel = connection.createChannel();
        //4：声明路由 绑定关系
        String exchangeName  = "test_fanout_exchange";

        // 5：发送
        String msg =" msg from Producer4FanOutExchange";
        String routingKye = "kaigejava";
        for(int i = 0;i<10;i++){
            channel.basicPublish(exchangeName,routingKye,null,msg.getBytes());
        }

        channel.close();
        connection.close();

    }
}
